package cn.zy.company.service;



import cn.zy.company.dao.NewsDao;
import cn.zy.company.entity.News;
import cn.zy.company.entity.PageObj;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsService {
    NewsDao newsDao=new NewsDao();
    public void add(News news) throws Exception {
        newsDao.add(news);
    }
    public void delete(Integer id) throws Exception {
        newsDao.delete(id);
    }
    public void update( News news) throws Exception {
        newsDao.update(news);
    }
    public PageObj<News> findById(Integer id) throws Exception {
        News news = newsDao.findById(id);
        PageObj<News> page = new PageObj<News>();
        List<News> list = new ArrayList<News>();
        list.add(news);
        page.setList(list);
        page.setPageCount(1);
        page.setCount(Long.valueOf(1));
        return page;
    }

    public PageObj<News> findAll(PageObj<News> page) throws Exception {
        List<News> all = newsDao.findAll(page.getPage(), page.getSize());
        page.setList(all);
        long l = newsDao.countAll();
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;
    }

    public PageObj<News> find(String name, PageObj<News> page) throws SQLException {
        List<News> all = newsDao.find(name,page.getPage(), page.getSize());
        page.setList(all);
        long l = newsDao.countByName(name);
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;
    }

    public PageObj<News> findByUserId(String userId, PageObj<News> page) throws SQLException {
        List<News> all = newsDao.findByUserId(userId,page.getPage(), page.getSize());
        page.setList(all);
        long l = newsDao.countByUserId(userId);
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;
    }

    public PageObj<News> findByDate(String date, PageObj<News> page) throws SQLException {
        List<News> all = newsDao.findByDate(date,page.getPage(), page.getSize());
        page.setList(all);
        long l = newsDao.countByDate(date);
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;
    }
}
